package com.gqzdev.xblog.controller.admin;


import com.gqzdev.xblog.page.Page;
import com.gqzdev.xblog.service.NewsuserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Newsuser)表控制层
 *
 * @author ganquanzhong
 * @since 2020-03-07 21:46:53
 */
@Controller
@RequestMapping("/admin/newsuser")
public class NewsuserController {
    /**
     * 服务对象
     */
    @Resource
    private NewsuserService newsuserService;

    /***************后台系统******************/
    /**
     * To用户列表页面
     */
    @RequestMapping(value="list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        Map<String, Object> queryMap = new HashMap<String, Object>();
        model.setViewName("newsuser/list");
        return model;
    }

    /**
     * 获取用户列表  和搜索
     */
    @RequestMapping(value="list",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(Page page,
                                       @RequestParam(name="username",required=false,defaultValue="") String username,
                                       @RequestParam(name="name",required=false,defaultValue="") String name){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("username", username);
        queryMap.put("name", name);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows",  newsuserService.findList(queryMap));
        ret.put("total", newsuserService.getTotal(queryMap));
        return ret;
    }


    /**
     * 批量删除日志
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(String ids){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(ids)){
            ret.put("type", "error");
            ret.put("msg", "选择要删除的数据！");
            return ret;
        }
        if(ids.contains(",")){
            ids = ids.substring(0,ids.length()-1);
        }
        if(newsuserService.delete(ids) <= 0){
            ret.put("type", "error");
            ret.put("msg", "用户删除失败，请联系管理员！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "用户删除成功！");
        return ret;
    }

    @RequestMapping(value="/check",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,String> check(String ids){
        Map<String,String> ret = new HashMap<String, String>();
        if(ids == null){
            ret.put("type", "error");
            ret.put("msg", "请选择要启动的用户！");
            return ret;
        }
        if(ids.contains(",")){
            ids = ids.substring(0,ids.length()-1);
        }
        if(newsuserService.check(ids) <= 0){
            ret.put("type", "error");
            ret.put("msg", "用户启动失败，请联系管理员！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "用户启用成功！");
        return ret;
    }

}